<?php
/*
 * This file is part of the BooXtore Student project.
 *
 * (c) Francis Backeland f.backeland@gmail.com
 * (c) Laurine Chaplot chaplot.laurine@orange.fr
 * (c) Julien Dutot julien.dutot@gmail.com
 * (c) Romain Guillaume romain-guillaume@orange.fr
 * (c) Guillaume Paringaux g.paringaux@gmail.com
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code
 * 
 */
namespace BXTR\AdminBundle\EventListener;

use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\Security\Core\SecurityContextInterface;
use BXTR\AdminBundle\Controller\InitializableEntityControllerInterface;
use BXTR\CoreBundle\EventListener\AbstractKernelControllerEventListener;

/**
 * Fire the setEntity method on the AdminBundle entities controllers.
 *
 * @see AbstractKernelControllerEventListener
 */
class InjectControllerEntityListener extends AbstractKernelControllerEventListener
{
    public function onKernelController(FilterControllerEvent $event)
    {
        if (!$this->controllerHasTag($event, 'tag.bxtr_admin.controller.entity') ) {
            return;
        }

        $controller = $event->getController();
        if (!$this->isControllerAnObject($controller)) { 
            return;
        }

        $controllerObject = $controller[0];

        $entityName = $event->getRequest()->get('entityName');
        if (!empty($entityName)) {
            $controllerObject->selectEntity($entityName);
        }
    }
}
